import { mergeRecursive } from "@/utils/ruoyi";
import dictConverter from './DictConverter'

export const options = {
  metas: {
    '*': {
      /**
       * 字典请求，方法签名为function(dictMeta: DictMeta): Promise
       */
      request: (dictMeta) => {
        console.log(`load dict ${dictMeta.type}`)
        return Promise.resolve([])
      },
      /**
       * 字典响应数据转换器，方法签名为function(response: Object, dictMeta: DictMeta): DictData
       */
      responseConverter,
      labelField: 'label',
      valueField: 'value',
    },
  },
  /**
   * 默认标签字段
   */
  DEFAULT_LABEL_FIELDS: ['label', 'name', 'title'],
  /**
   * 默认值字段
   */
  DEFAULT_VALUE_FIELDS: ['value', 'id', 'uid', 'key'],
  // 添加本地字典配置
  localDict: {
    // 业务场景字典
    business_scene: [
      {
        label: '自营',
        value: '0',
        listClass: ''
      },
      {
        label: '中医下乡场景',
        value: '1',
        listClass: ''
      },
      {
        label: '远程问诊场景',
        value: '2',
        listClass: ''
      }
    ],
    // 更新状态字典
    update_status: [
      {
        label: '成功',
        value: '1',
        listClass: 'success'
      },
      {
        label: '失败',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 报告状态字典
    report_status: [
      {
        label: '未出报告',
        value: '0',
        listClass: 'danger'
      },
      {
        label: '报告生成中',
        value: '1',
        listClass: 'primary'
      },
      {
        label: '已出报告',
        value: '2',
        listClass: 'success'
      }
    ],
    // 审核状态字典
    dp_review_status: [
      {
        label: '审核中',
        value: '0',
        listClass: 'warning'
      },
      {
        label: '已通过',
        value: '1',
        listClass: 'success'
      },
      {
        label: '已驳回',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 合作审核状态字典
    cooperation_status: [
      {
        label: '待审核',
        value: '0',
        listClass: 'warning'
      },
      {
        label: '已通过',
        value: '1',
        listClass: 'success'
      },
      {
        label: '已拒绝',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 认证审核状态字典
    verify_status: [
      {
        label: '已申请',
        value: '0',
        listClass: 'warning'
      },
      {
        label: '已通过',
        value: '1',
        listClass: 'success'
      },
      {
        label: '已驳回',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 支付方式字典
    pay_method: [
      {
        label: '微信',
        value: '1',
        listClass: 'success'
      },
      {
        label: '银联',
        value: '2',
        listClass: ''
      },
      {
        label: '支付宝',
        value: '3',
        listClass: 'primary'
      }
    ],
    // 提现审核状态字典
    withdrawal_status: [
      {
        label: '审核中',
        value: '0',
        listClass: 'warning'
      },
      {
        label: '审核通过',
        value: '1',
        listClass: 'success'
      },
      {
        label: '审核拒绝',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 加工费收取方式字典
    fee_type: [
      {
        label: '固定费用',
        value: '0',
        listClass: ''
      },
      {
        label: '按加工单位收费',
        value: '1',
        listClass: ''
      }
    ],
    // 订单类型字典
    order_source: [
      {
        label: '宜健订单',
        value: '0',
        listClass: ''
      },
      {
        label: '中医订单',
        value: '1',
        listClass: ''
      }
    ],
    // 审核状态字典
    audit_status: [
      {
        label: '审核通过',
        value: '1',
        listClass: 'success'
      },
      {
        label: '审核驳回',
        value: '2',
        listClass: 'danger'
      }
    ],
    // 合作模式字典
    cooperate_type: [
      {
        label: '统一定价返佣',
        value: '1',
        listClass: ''
      },
      {
        label: '采购价结算',
        value: '2',
        listClass: ''
      }
    ],
    // 控价状态字典
    price_control: [
      {
        label: '控价',
        value: '1',
        listClass: 'success'
      },
      {
        label: '不控价',
        value: '0',
        listClass: 'info'
      }
    ],
    // 团队身份字典
    team_leader: [
      {
        label: '负责人',
        value: '1',
        listClass: 'success'
      },
      {
        label: '成员',
        value: '0',
        listClass: 'info'
      }
    ],
  }
}

/**
 * 映射字典
 * @param {Object} response 字典数据
 * @param {DictMeta} dictMeta 字典元数据
 * @returns {DictData}
 */
function responseConverter(response, dictMeta) {
  const dicts = response.content instanceof Array ? response.content : response
  if (dicts === undefined) {
    console.warn(`no dict data of "${dictMeta.type}" found in the response`)
    return []
  }
  return dicts.map(d => dictConverter(d, dictMeta))
}

export function mergeOptions(src) {
  mergeRecursive(options, src)
}

export default options
